package com.shinowit.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shinowit.dao.mapper.MerchandiseInfoMapper;
import com.shinowit.model.DataGridResult;
import com.shinowit.model.MerchandiseInfo;
import com.shinowit.model.MerchandiseInfoCriteria;
import com.shinowit.model.Result;
import com.shinowit.tools.LetterUtil;

@Controller
@RequestMapping(value="/system/merchandise_info")
public class MerchandiseInfoController {

	@Resource
	private MerchandiseInfoMapper merchandiseInfoMapper_dao;
	
	@Resource
	private JdbcTemplate jt;
	
	@RequestMapping(value="/page")
	@ResponseBody
	public  DataGridResult<MerchandiseInfo> page(@RequestParam("page") Integer pageIndex,@RequestParam("rows") Integer pageSize,MerchandiseInfo query_data,String sort,String order){
		DataGridResult<MerchandiseInfo> result=new DataGridResult<MerchandiseInfo>();
		
		MerchandiseInfoCriteria ex=new MerchandiseInfoCriteria();
		
		ex.setPageIndex(pageIndex);
		ex.setPageSize(pageSize);
		
		if ((null!=sort) && (sort.trim().length()>0)){
			ex.setOrderByClause(sort);
		}
		
		if ((null!=order) && (order.trim().length()>0)){
			if (!"asc".equalsIgnoreCase(order)){
				ex.setOrderByClause(ex.getOrderByClause()+" desc");
			}
		}
		
		List<MerchandiseInfo> rows=merchandiseInfoMapper_dao.selectByExamplePage(ex);
		
		long record_count=merchandiseInfoMapper_dao.countByExample(ex);
		result.setTotal(record_count);
		result.setRows(rows);
		return result;
	}
	
	@RequestMapping(value={"","/"})
	public String openListpage(){
		return "/product/list";
	}
	
	
	//取分类数据
	@RequestMapping("/get_type")
	@ResponseBody
	public List<Map<String,Object>> getTypeList(){
		return jt.queryForList("select type_code as id,type_name as text from merchandise_type_info where status=1");
	}

	//取促销状态数据
	@RequestMapping("/get_sale_status")
	@ResponseBody
	public List<Map<String,Object>> getSaleStatusList(){
		return jt.queryForList("select sale_status_id as id,sale_status_name as text from sale_status where status=1");
	}

	//取促销状态数据
	@RequestMapping("/get_unit_type")
	@ResponseBody
	public List<Map<String,Object>> getUnitTypeList(){
		return jt.queryForList("select unit_id as id,unit_name as text from merchandise_unit_info where status=1");
	}
	
	@RequestMapping(value="/create")
	@ResponseBody
	public Result create_new(MerchandiseInfo data){
		Result result=new Result();
		data.setMerchandiseAb(LetterUtil.getAllFirstLetter(data.getMerchandiseName()));
		int i=merchandiseInfoMapper_dao.insert(data);
		if (1==i){
			result.setMsg("保存成功!");
			result.setStatus(true);
		}else{
			result.setMsg("保存失败!");
			result.setStatus(true);
		}
		return result;
	}	
	
	@RequestMapping(value="/edit")
	@ResponseBody
	public Result edit_data(MerchandiseInfo data){
		Result result=new Result();
		data.setMerchandiseAb(LetterUtil.getAllFirstLetter(data.getMerchandiseName()));
		int i=merchandiseInfoMapper_dao.updateByPrimaryKey(data);
		if (1==i){
			result.setMsg("修改成功!");
			result.setStatus(true);
		}else{
			result.setMsg("修改失败!");
			result.setStatus(true);
		}
		return result;
	}		
	
}
